
<?php

include('ConnectBdd.php');

// récup de l'id du tournoi de l'année en cours
    $reqP="Select Num_Tournois From volley_tournois Where Annees_Tournois= ".date("Y");
    $result = $connexion->query($reqP);
     while ($row=$result->fetch(PDO::FETCH_ASSOC))
     {
         $idTournoi = $row['Num_Tournois'];
     }

?>
    
    <center>
         <img src="./image/result_Class.png" height="150px" width="600px"/>
         <br>
         Sélèctionnez une poule: 
        <?php if (isset($_POST['choixPoule']))       
               {             
           ?>   
         <select id="choixPoule" name="choixPoule" class="log" style="width: 190px; height:30px;" onchange="AfficherMatch()">
              <?php
                                    // On récupère les poules existantes
                              $reqP="Select p.Id_Poule, Nom_Poule 
                                     From volley_poule p, volley_rattacher r, volley_tournois t 
                                     Where p.Id_Poule = r.Id_Poule AND
                                     r.Num_Tournois = t.Num_Tournois AND 
                                     InscriptionFermee=0 AND 
                                     Annees_Tournois= ".date("Y");
                                   $result = $connexion->query($reqP);
                              while ($row=$result->fetch(PDO::FETCH_ASSOC))
                        {
                                   if($row['Id_Poule']==$_POST['choixPoule']){
                                        echo "<option value = '".$row['Id_Poule']."' selected>".$row['Nom_Poule']."</option>";
                                   }else{
                                        echo "<option value = '".$row['Id_Poule']."'>".$row['Nom_Poule']."</option>";
                                   }
                        }
                ?>
         </select>
           <?php } else { ?>
         <select id="choixPoule" name="choixPoule" class="log" style="width: 190px; height:30px;" onchange="AfficherMatch()">
              <option value="1">Choisir</option>
              <?php
                                    // On récupère les poules existantes
                              $reqP="Select p.Id_Poule, Nom_Poule 
                                     From volley_poule p, volley_rattacher r, volley_tournois t 
                                     Where p.Id_Poule = r.Id_Poule AND
                                     r.Num_Tournois = t.Num_Tournois AND 
                                     InscriptionFermee=0 AND 
                                     Annees_Tournois= ".date("Y");
                              $result = $connexion->query($reqP);
                              while ($row=$result->fetch(PDO::FETCH_ASSOC))
                        {
                                echo "<option value = '".$row['Id_Poule']."'>".$row['Nom_Poule']."</option>";
                        }
                ?>
         </select>
          <?php } ?>          
         <br>
         <br>
    <?php
            //on affiche les matchs correspondant à la poule sélectionnée
        if(isset($_POST['choixPoule'])){
                      
            $req="SELECT dom.Libelle_Equipe eq1,Ext.Libelle_Equipe eq2,Score_Set1_Dom,Score_Set1_Ext,Score_Set2_Dom,Score_Set2_Ext,Score_Set3_Dom,Score_Set3_Ext,Score_Set4_Dom,Score_Set4_Ext,Score_Set5_Dom,Score_Set5_Ext,Date_Match,Libelle_Plage
                   FROM volley_equipe dom, volley_equipe ext, volley_match,volley_plage, volley_inscrire i
                   WHERE dom.Id_Equipe=Id_Equipe_dom
                   and ext.Id_Equipe=Id_Equipe_ext
                   and volley_plage.Id_Plage=volley_match.Id_Plage
                   and i.Id_Equipe=dom.Id_Equipe
                   and Id_Poule='".$_POST['choixPoule']."'";
        
        $result = $connexion->query($req);
       ?>  
         
         <div class="tableau">
             
    <table cellspacing="0" >
        <tr>
            <td>Equipe 1  </td>
            <td>Equipe 2</td>
            <td>score set 1</td>
            <td>score set 2</td>
            <td>score set 3</td>
            <td>score set 4</td>
            <td>score set 5</td>
            <td>Vainqueur</td>
            <td>date</td>
            <td>plage</td>
        </tr>
     <?php
     
     $result = $connexion->query($req);
            while ($row=$result->fetch(PDO::FETCH_ASSOC)){
                ?>
        <tr>
            <?php 
                $NbSetEq1=0;
                   $NbSetEq2=0;

                   if($row['Score_Set1_Dom'] > $row['Score_Set1_Ext']){
                      $NbSetEq1+=1;
                   }

                   else if($row['Score_Set1_Dom'] < $row['Score_Set1_Ext']){
                        $NbSetEq2+=1;
                   }

                    if($row['Score_Set2_Dom'] > $row['Score_Set2_Ext']){
                      $NbSetEq1+=1;
                   }
                   else  if($row['Score_Set2_Dom'] < $row['Score_Set2_Ext']){
                        $NbSetEq2+=1;
                   }

                    if($row['Score_Set3_Dom'] > $row['Score_Set3_Ext']){
                      $NbSetEq1+=1;
                   }
                   else  if($row['Score_Set3_Dom'] < $row['Score_Set3_Ext']){
                        $NbSetEq2+=1;
                   }

                    if($row['Score_Set4_Dom'] > $row['Score_Set4_Ext']){
                      $NbSetEq1+=1;
                   }
                   else  if($row['Score_Set4_Dom'] < $row['Score_Set4_Ext']){
                        $NbSetEq2+=1;
                   }

                    if($row['Score_Set5_Dom'] > $row['Score_Set5_Ext']){
                      $NbSetEq1+=1;
                   }
                   else  if($row['Score_Set5_Dom'] < $row['Score_Set5_Ext']){
                        $NbSetEq2+=1;
                   }

                  if ($NbSetEq1 > $NbSetEq2){
                      $vainqueur= $row['eq1'];
                  }
                  else if($NbSetEq1 < $NbSetEq2) {
                      $vainqueur=$row['eq2'];
                  }
                  else {
                     $vainqueur="";
                  }
               
                    echo "<td>" . $row['eq1'] . "</td>";
                    echo "<td>" . $row['eq2'] . "</td>";
                    echo "<td>" . $row['Score_Set1_Dom'] ."/". $row['Score_Set1_Ext'] ."</td>";
                    echo "<td>" . $row['Score_Set2_Dom'] ."/". $row['Score_Set2_Ext'] ."</td>";
                    echo "<td>" . $row['Score_Set3_Dom'] ."/". $row['Score_Set3_Ext'] ."</td>";
                    echo "<td>" . $row['Score_Set4_Dom'] ."/". $row['Score_Set4_Ext'] ."</td>";
                    echo "<td>" . $row['Score_Set5_Dom'] ."/". $row['Score_Set5_Ext'] ."</td>";
                    echo "<td>".$vainqueur."</td>";
                    echo "<td>" . $row['Date_Match'] . "</td>";
                    echo "<td>" . $row['Libelle_Plage'] . "</td>";
                ?>
        </tr>
        <?php
        }
            ?>
        
    </table>
    </div>
          <?php
        }
            ?>
    
    </center>

